//
//  LibraryViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/15.
//  Copyright © 2018年 cxd. All rights reserved.
//  图书

#import "LibraryViewController.h"
#import "BookCollectCell.h"
#import "FooterCollectionReusableView.h"
#import "BookDetailShowViewController.h"
#import "ShelfBookModel.h"
#import "LoginViewController.h"

@interface LibraryViewController () <UICollectionViewDelegate,UICollectionViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property (nonatomic, strong)UICollectionView *bookCollectionView;
@property (nonatomic, strong)NSArray *dataArray;

@end

@implementation LibraryViewController
{
    BOOL requestCompleted;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getNetData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self getNetData];
    requestCompleted = nil;
    [self buildUI];
}

- (void)getNetData {
//    if ([NSString checkNullWithNameString:[CXDHelper getUserAccountName]]) {
//
//        LoginViewController *loginVC = [[LoginViewController alloc] init];
//        loginVC.backHome = YES;
//        [self.navigationController pushViewController:loginVC animated:YES];
//        
//        return;
//    }
    __weak typeof(self) weakSelf = self;
    [CXDNetHelp getRequestWithURLString:[NSString stringWithFormat:@"%@%@",CollectionBookURL,[CXDHelper getUserAccountName]] parameters:nil success:^(id  _Nullable responseObject) {
        requestCompleted = YES;
        [self.bookCollectionView reloadEmptyDataSet];
        _dataArray = [ShelfBookModel mj_objectArrayWithKeyValuesArray:responseObject];
        [weakSelf.bookCollectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        requestCompleted = YES;
        [self.bookCollectionView reloadEmptyDataSet];
    }];
}

- (void)buildUI {
    [self.view addSubview:self.bookCollectionView];
    self.bookCollectionView.emptyDataSetSource = self;
    self.bookCollectionView.emptyDataSetDelegate = self;
}

#pragma mark - 懒加载
- (UICollectionView *)bookCollectionView {
    if (!_bookCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth - 30) / 3, 150);
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        _bookCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight - 49) collectionViewLayout:layout];
        _bookCollectionView.delegate = self;
        _bookCollectionView.dataSource = self;
        _bookCollectionView.backgroundColor = RGB(238, 238, 238);
        _bookCollectionView.showsVerticalScrollIndicator = NO;
        _bookCollectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        [_bookCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
    }
    return _bookCollectionView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    ShelfBookModel *tempModel = _dataArray[indexPath.item];
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootHomeURL,tempModel.spic]]];
    cell.nameLabel.text = tempModel.title;
    cell.backView.hidden = YES;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([CXDHelper getUserLoginStatus] || ![NSUserDefaults.standardUserDefaults boolForKey:K_NeedLogin]) {
        BookDetailShowViewController *detailVC = [[BookDetailShowViewController alloc] init];
        ShelfBookModel *tempModel = _dataArray[indexPath.item];
        detailVC.bookId = tempModel.pid;
        detailVC.navigationItem.title = tempModel.title;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        [CXDNoticeView showNoticeViewWithString:@"请先进行登录"];
    }
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return requestCompleted && self.dataArray.count == 0;
}

@end
